<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Admin\CarouselImage;

class CarouselImageController extends Controller
{
    /**
     * @param $type_id
     * @param $type 1:課程表輪播圖  2：科室表輪播圖
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index($type_id, $type)
    {

        $result = [
            'result'=>CarouselImage::lists($type_id, $type),
            'type_id'=>$type_id,
            'type'=>$type,
        ];

        return view('admin.carousel_image.index', $result);
    }

    public function add($type_id, $type)
    {

        $result = [
            'type_id'=>$type_id,
            'type'=>$type,
        ];

        return view('admin.carousel_image.edit',$result);
    }

    public function edit(Request $request, $id, $type_id, $type)
    {

        $info = CarouselImage::info($id);

        $result = [
            'id'=>$id,
            'type_id'=>$type_id,
            'type'=>$type,
            'result'=> $info,
        ];

        return view( 'admin.carousel_image.edit', $result);

    }

    public function del($id)
    {

        CarouselImage::del($id);

        return self::success([]);

    }

    public function do(Request $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $image_avatar = !empty($request->file('image')) ? $request->file('image') : [];

            if (!empty($image_avatar)) {

                $path = $image_avatar->store('images/'. date('Ymd'));

                $input['image'] = "/upload/".$path;

            } else {
                unset($input['image']);
            }

            $input[CarouselImage::type_result($input['type'])] = $input['type_id'];
            unset($input['type_id']);

            if (empty($input['id'])) {
                unset($input['id']);
                CarouselImage::add($input);
            } else {
                CarouselImage::edit($input['id'],$input);
            }

            return self::success([]);
        }
    }
}
